<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上级工作中心" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid" :hidden="parentWorkHidden">
              <j-dict-select-tag type="list" v-model="model.pid" dictCode="mdm_work_center,name,id" placeholder="请选择父级工作中心" :disabled="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shortName">
              <a-input v-model="model.shortName" placeholder="请输入简称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productLineId">
              <j-dict-select-tag type="list" v-model="model.productLineId" dictCode="mdm_product_line,name,id"
                                 placeholder="请选择产品线"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factoryId">
              <j-dict-select-tag type="list" v-model="model.factoryId" dictCode="mdm_factory,factory_name,id" placeholder="请选择工厂" @init="factoryChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyId">
              <j-dict-select-tag type="list" v-model="model.companyId" dictCode="mdm_company,company_name,id" placeholder="请选择公司" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编制人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postNum">
              <a-input-number v-model="model.postNum" placeholder="请输入编制人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="操作范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workScope">
              <j-dict-select-tag type="list" v-model="model.workScope" dictCode="mdm_work_scope" placeholder="请选择操作范围" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所在位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
              <a-input v-model="model.location" placeholder="请输入所在位置"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="零配件提取天数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partDays">
              <a-input-number v-model="model.partDays" placeholder="请输入零配件提取天数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="零配件下工单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partOrderFlag">
              <j-switch v-model="model.partOrderFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="检修计划下工单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repairOrderFlag">
              <j-switch v-model="model.repairOrderFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="扫码方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scanWay">
              <j-dict-select-tag type="list" v-model="model.scanWay" dictCode="scan_way" placeholder="请选择扫码方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectiveDate">
              <j-date placeholder="请选择生效日期" v-model="model.effectiveDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expireDate">
              <j-date placeholder="请选择失效日期" v-model="model.expireDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-input v-model="model.note" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MdmWorkCenterForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      initAddModel: {
        type: Object
      }
    },
    data () {
      return {
        model:{
            workScope:"99",
            partOrderFlag:"0",
            repairOrderFlag:"0",
            scanWay:"99",
            enableFlag:"1",
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        parentWorkHidden: false,
        validatorRules: {
          code: [
            { required: true, message: '请输入编码!'},
          ],
          name: [
            { required: true, message: '请输入名称!'},
          ],
        },
        url: {
          add: "/mdm/workCenter/add",
          edit: "/mdm/workCenter/edit",
          queryById: "/mdm/workCenter/queryById",
          queryFactoryById: "/mdm/factory/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(Object.assign({},this.model, this.initAddModel)));
    },
    methods: {
      add (pid) {
        if(pid){
          this.parentWorkHidden = false;
        }else{
          this.parentWorkHidden = true;
        }
        this.edit(Object.assign({},this.modelDefault,{pid: pid}));
      },
      edit (record) {
        if(record.pid){
          this.parentWorkHidden = false;
        }else{
          this.parentWorkHidden = true;
        }
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.nameCode = this.model.name + '(' + this.model.code + ')'
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      factoryChange(factoryId){
        if(!factoryId){
          this.model.companyId = '';
          return;
        }
        getAction(this.url.queryFactoryById, {id: factoryId}).then(res =>{
          if(res.success){
            let factoryData = res.result;
            this.model.companyId = factoryData.companyId;
          }else{
            this.$message.warning(res.message);
          }
        }).finally(() => {
          //
        })
      }
    }
  }
</script>